package com.dlc.shop.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dlc.shop.allinpay.service.AllinpayCompanyService;
import com.dlc.shop.bean.dto.ShopAuditingInfoDto;
import com.dlc.shop.bean.enums.ShopStatus;
import com.dlc.shop.bean.model.ShopAuditing;
import com.dlc.shop.bean.param.AuditingInfoParam;
import com.dlc.shop.common.allinpay.constant.AllinpayShopStatus;
import com.dlc.shop.common.allinpay.constant.CompanyInfoProcessStatus;
import com.dlc.shop.common.allinpay.constant.PaySysType;
import com.dlc.shop.dao.ShopAuditingMapper;
import com.dlc.shop.service.ShopAuditingService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 商家审核信息
 *
 * @author Dwl
 * @date 2019-09-19 14:02:57
 */
@Service
@RequiredArgsConstructor
public class ShopAuditingServiceImpl extends ServiceImpl<ShopAuditingMapper, ShopAuditing> implements ShopAuditingService {

    private final ShopAuditingMapper shopAuditingMapper;
    private final AllinpayCompanyService allinpayCompanyService;


    @Override
    public ShopAuditing getShopAuditingByUserId(String userId) {
        return shopAuditingMapper.getShopAuditingByUserId(userId);
    }

    @Override
    public IPage<ShopAuditingInfoDto> auditingInfoList(Page<ShopAuditingInfoDto> page, AuditingInfoParam auditingInfoParam) {
        Integer paySysType = allinpayCompanyService.getIsAllinpay() ? PaySysType.ALLINPAY.value() : PaySysType.DEFAULT.value();
        auditingInfoParam.setPaySysType(paySysType);
        if (CollUtil.isNotEmpty(auditingInfoParam.getShopStatusList())) {
            auditingInfoParam.setShopStatus(auditingInfoParam.getShopStatusList().get(0));
        }
        IPage<ShopAuditingInfoDto> auditingPage = shopAuditingMapper.auditingInfoList(page, auditingInfoParam);
        boolean isAllinpay = allinpayCompanyService.getIsAllinpay();
        for (ShopAuditingInfoDto auditingInfoDto : auditingPage.getRecords()) {
            if (isAllinpay) {
                auditingInfoDto.setShopStatus(getAllinpayShopStatus(auditingInfoDto.getAllinpayShopStatus(), auditingInfoDto.getShopStatus(), auditingInfoDto.getCompanyInfoProcessStatus()));
            }
            if (StrUtil.isNotBlank(auditingInfoDto.getMobile())) {
                auditingInfoDto.setMobile(PhoneUtil.hideBetween(auditingInfoDto.getMobile()).toString());
            }
        }
        return auditingPage;
    }

    private static Integer getAllinpayShopStatus(Integer allinpayShopStatus, Integer shopStatus, Integer companyInfoProcessStatus) {
        boolean offline = Objects.equals(allinpayShopStatus, AllinpayShopStatus.UN_AVAILABLE.value())
                && (!Objects.equals(shopStatus, ShopStatus.APPLYING.value()) && !Objects.equals(shopStatus, ShopStatus.OPEN_AWAIT_AUDIT.value()));
        if (Objects.equals(allinpayShopStatus, AllinpayShopStatus.WAIT_AUDIT.value())) {
            shopStatus = ShopStatus.OFFLINE_AUDIT.value();
        }
        if (Objects.equals(allinpayShopStatus, AllinpayShopStatus.OPEN_WAIT_AUDIT.value())) {
            shopStatus = ShopStatus.OPEN_AWAIT_AUDIT.value();
        }
        if (Objects.equals(allinpayShopStatus, AllinpayShopStatus.OFFLINE.value()) || offline) {
            shopStatus = ShopStatus.OFFLINE.value();
        }
        if (Objects.equals(companyInfoProcessStatus, CompanyInfoProcessStatus.FAIL.value())
                && Objects.equals(allinpayShopStatus, AllinpayShopStatus.OFFLINE.value())) {
            shopStatus = ShopStatus.AUDIT_FAIL.value();
        }
        return shopStatus;
    }

    @Override
    public void updateToFail(List<Long> shopIds) {
        shopAuditingMapper.updateToFail(shopIds);
    }

}
